import { Material, Mesh, Object3D, Vector } from 'three';
import { Command } from '../command.js';
import { Editor } from '../editor.js';

class SetMaterialVectorCommand<K extends keyof Material> extends Command {
	object:Mesh;
	material:Material;
	oldValue:Vector|undefined;
	newValue:Vector;
	attributeName:K;
	constructor( editor:Editor, object:Object3D, attributeName:K, newValue:Vector, materialSlot:number ) {

		super( editor );

		this.type = 'SetMaterialColorCommand';
		this.name = `Set Material.${attributeName}`;
		this.updatable = true;

		this.object = object as Mesh;
		this.material = this.editor.getObjectMaterial( this.object, materialSlot );

		this.oldValue = ( this.material !== undefined ) ? this.material[ attributeName ].toArray() : undefined;
		this.newValue = newValue;

		this.attributeName = attributeName;

	}

	execute() {

		this.material[ this.attributeName ].fromArray( this.newValue );

		this.editor.signals.materialChanged.dispatch( this.material );

	}

	undo() {

		this.material[ this.attributeName ].fromArray( this.oldValue );

		this.editor.signals.materialChanged.dispatch( this.material );

	}

	update( cmd:SetMaterialVectorCommand<K> ) {

		this.newValue = cmd.newValue;

	}


}

export { SetMaterialVectorCommand };
